package Client.Component;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class WorkSpace extends JPanel {
    //绘图板
    private final DrawPanel drawPanel = new DrawPanel();
    private HashMap<ColorButton, Color> bgColors = new HashMap<>();
    private HashMap<ColorButton, Color> drawColors = new HashMap<>();

    public WorkSpace() {
        this.setLayout(null);

        if(GameFrame.status.equals("DRAW")){
            addBgButton(bgColors);
            addDrawButton(drawColors);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //添加绘图控件
        drawPanel.setBounds(30, 0, this.getWidth() - 60, this.getHeight() - 30);
        this.add(drawPanel);
    }

    private void addBgButton(HashMap<ColorButton, Color> colors) {
        bgColors.put(new ColorButton(Color.pink), Color.pink);
        bgColors.put(new ColorButton(Color.lightGray), Color.lightGray);
        bgColors.put(new ColorButton(Color.DARK_GRAY), Color.DARK_GRAY);
        bgColors.put(new ColorButton(Color.WHITE), Color.WHITE);

        int i = 0;
        for (ColorButton b : colors.keySet()) {
            b.setColor(colors.get(b));
            b.setBounds(5, 5 + (i * (25 + 20)), 20, 25);
            this.add(b);
            b.addActionListener(e -> {
                drawPanel.setPanelColor(colors.get(b));
                drawPanel.repaint();
            });
            i++;
        }
    }

    private void addDrawButton(HashMap<ColorButton, Color> colors) {
        drawColors.put(new ColorButton(Color.pink), Color.pink);
        drawColors.put(new ColorButton(Color.lightGray), Color.lightGray);
        drawColors.put(new ColorButton(Color.DARK_GRAY), Color.DARK_GRAY);
        drawColors.put(new ColorButton(Color.WHITE), Color.WHITE);

        int i = 0;
        for (ColorButton b : colors.keySet()) {
            b.setColor(colors.get(b));
            b.setBounds(580, 5 + (i * (25 + 20)), 20, 25);
            this.add(b);
            b.addActionListener(e -> {
                drawPanel.setDrawColor(colors.get(b));
                drawPanel.repaint();
            });
            i++;
        }
    }

    public DrawPanel getDrawPanel() {
        return drawPanel;
    }
}
